import isEmpty from 'lodash/isEmpty'
import { ElMessage } from 'element-plus'
import { translateTitle } from '@/utils/i18n'
// import isString from 'lodash/isString'
//必须带done回调 执行完成调用
export default function valueToComponent({ vm, action, additionalData, done }) {
  console.log(vm, action, additionalData)
  if (!additionalData) {
    ElMessage({
      showClose: true,
      message: translateTitle('Ve.eventTask.动作只能由表格事件触发'),
      type: 'error',
    })
    done(null, {})
    return
  }
  let _valueToComponentData = action.extension.valueToComponentData.value
  let _event = _valueToComponentData.valueToComponentInfo
  let _value = additionalData.data

  if (additionalData.data.length > 0) {
    let _v = isEmpty(_event.valueKey) ? _value[0] : _value[0][_event.valueKey]
    let _label = isEmpty(_event.labelKey) ? _v : _value[0][_event.labelKey]

    let _component = vm.vuiComponents[_event.value[0]] && vm.vuiComponents[_event.value[0]].self
    if (!_component) {
      ElMessage({
        showClose: true,
        message: translateTitle('Ve.eventTask.不能给此组件赋值'),
        type: 'error',
      })
    } else {
      //下拉打开页面组件
      if (_component.getInfo.type == 'uiSelectLayer') {
        _component.setFormDataModel(_v)
        _component.setLabel(_label)
      } else if (_component.getInfo.type == 'uiForm') {
        _component.setFormData(_v)
      } else {
        _component.setFormDataModel(_v)
      }
    }
  }

  done(null, {})
}
